package com.itheima.springbootwebtlias.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
// 1.自定义拦截器类，实现HandlerInterceptor接口
// 3.配置拦截器，交给容器管理
@Component
public class MyInterceptor implements HandlerInterceptor {

    // 2.重写方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截器拦截前...");
        // 1.登录验证，权限校验，敏感数据检测，字符编码统一处理

        // 2.放行，不放行
        return true;    // true：放行，false：不放行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("拦截器拦截后1...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("拦截器拦截后2...");
    }
}
